package com.seecen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.Topic;
import com.seecen.pojo.vo.PortalVo;
import com.seecen.service.TopicService;
import com.seecen.mapper.TopicMapper;
import com.seecen.utils.JwtHelper;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 桃子
* @description 针对表【pet_topic】的数据库操作Service实现
* @createDate 2025-07-21 14:26:52
*/
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic>
    implements TopicService{

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private JwtHelper jwtHelper;

    //获取帖子, 该功能还未完全实现
//    @Override
//    public Result findTopicPage(PortalVo portalVo) {
//        Page<Map> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
//        topicMapper.selectPageByTopic(page,portalVo);
//
//        List<Map> records = page.getRecords();
//
//        Map pageInfo= new HashMap();
//        pageInfo.put("pageData",records);
//        pageInfo.put("pageNum",page.getCurrent());
//        pageInfo.put("pageSize",page.getSize());
//        pageInfo.put("totalPage",page.getPages());
//        pageInfo.put("totalSize",page.getTotal());
//
//        Map data = new HashMap();
//        data.put("pageInfo",pageInfo);
//
//        return Result.ok(data);
//    }

    //获取帖子
    @Override
    public Result showTopicDetail(Integer topicId) {

        // 1. 参数校验
        if (topicId == null || topicId <= 0) {
            return Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }

        // 2. 查询帖子详情
        Map<String, Object> topic = topicMapper.queryTopicDetailByTopicId(topicId);
        if (topic == null || topic.isEmpty()) {
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }

        // 3. 准备更新阅读数
        try {
            // 获取必要字段，提供默认值
            Integer viewCount = topic.get("pageViews") != null ? (Integer) topic.get("pageViews") : 0;
            Integer version = topic.get("version") != null ? (Integer) topic.get("version") : null;
            Integer id = topic.get("topicId") != null ? (Integer) topic.get("topicId") : null;

            if (id == null) {
                log.error("帖子ID为空，无法更新阅读数");
                return Result.ok(topic);
            }

            Topic updateTopic = new Topic();
            updateTopic.setTopicId(id);
            updateTopic.setPageViews(viewCount + 1);

            // 如果有版本控制
            if (version != null) {
                updateTopic.setVersion(version);
            }

            // 4. 执行更新
            LambdaUpdateWrapper<Topic> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Topic::getTopicId, id);
            // 如果有版本控制，添加版本条件
            if (version != null) {
                updateWrapper.eq(Topic::getVersion, version);
            }

            int updated = topicMapper.update(updateTopic, updateWrapper);

            // 乐观锁更新失败处理
            if (updated == 0) {
                log.warn("帖子阅读数更新失败，可能已被修改 topicId: {}");
            }
        } catch (Exception e) {
            log.error("更新帖子阅读数异常", e);
            // 这里可以选择是否抛出异常，或者只是记录日志继续返回数据
        }

        // 5. 返回结果
//        Map<String, Object> data = new HashMap<>();
//        data.put("topic", topic);
        return Result.ok(topic);
    }

    @Override
    public Result findAllTopic() {
        List<Topic> topics = topicMapper.selectList(null);
        return Result.ok(topics);
    }
}




